package com.mallcai.bigdata.ladon.service.report.impl;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.cake.bigdata.starter.exception.error.CommonErrorCode;
import com.mallcai.bigdata.ladon.client.service.IReportDataQueryService;
import com.mallcai.bigdata.ladon.client.vo.ReportDataVO;
import com.mallcai.bigdata.ladon.client.param.ReportQueryParam;
import com.mallcai.bigdata.ladon.dpl.entity.report.Report;
import com.mallcai.bigdata.ladon.dpl.entity.report.ReportSql;
import com.mallcai.bigdata.ladon.service.report.IReportObjectQueryService;
import com.mallcai.bigdata.ladon.service.report.impl.handler.support.ParamNameParser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * 需要提供dubbo接口时，放开@Service注释
 *
 * @author feixy
 * @version 1.0
 * @date 2019-07-18 14:15
 */
//@Service(interfaceClass = IReportDataQueryService.class, version = "1.0.0")
@Component
@Slf4j
public class ReportDataQueryService implements IReportDataQueryService {

    @Autowired
    private ReportQueryPipeline reportQueryPipeline;

    @Autowired
    private IReportObjectQueryService iReportObjectQueryService;

    @Override
    public Set<String> parseParamName(String reportCode) {
        if (reportCode == null) {
            throw new BusinessException(String.format("非法的参数:报表code为空"));
        }
        Report report = iReportObjectQueryService.getReportByCode(reportCode);
        if (report == null) {
            throw new BusinessException(String.format("非法的参数:不存在[code:%s]对应的报表", reportCode));
        }
        if (report.getDetailSqlId() == null) {
            throw new BusinessException(String.format("非法的参数:报表[code:%s]没有配置对应的sql", reportCode));
        }
        ReportSql reportSql = iReportObjectQueryService.getReportSqlById(report.getDetailSqlId());
        if (report == null || StringUtils.isBlank(reportSql.getText())) {
            throw new BusinessException(String.format("非法的参数:报表[code:%s]未配置sql", reportCode));
        }
        return ParamNameParser.parseUniqueParamName(reportSql.getText());
    }

    @Override
    public ReportDataVO queryData(ReportQueryParam reportParam, Map<String, Object> params) {
        try {
            String appName = reportParam.getAppName();
            if (StringUtils.isBlank(appName)) {
                throw new BusinessException("非法的参数:appName不能为空");
            }
            return reportQueryPipeline.doQuery(reportParam, params);
        } catch (Exception e) {
            log.error("查看报表[appName:{},code:{}]出错:", reportParam.getAppName(), reportParam.getReportCode(), e);
            String code = CommonErrorCode.EXCEPTION.getCode();
            String message = CommonErrorCode.EXCEPTION.getMessage();
            if (e instanceof BusinessException) {
                message = e.getMessage();
                code = ((BusinessException) e).getCode();
            }
            throw new BusinessException(code, message);
        }
    }

}
